using System;
using System.ComponentModel;
using System.Linq;

namespace SolutionUsingAsync.ViewModel
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            VerifyPropertyName(propertyName);

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void VerifyPropertyName(string propertyName)
        {
            var propertyInfos = this.GetType().GetProperties();
            if (!propertyInfos.Any(propertyInfo => propertyInfo.Name == propertyName))
                throw new Exception("Property not found " + propertyName);
        }

        public bool IsValidProperty(string propertyName)
        {
            var propertyInfos = this.GetType().GetProperties();
            if (propertyInfos.Any(propertyInfo => propertyInfo.Name == propertyName))
                return true;
            return false;
        }
    }
}
